
package com.winning.sx.microframework.controller.analysis;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.winning.dapi.data.entity.DataTable;
import com.winning.sx.microframework.common.util.FileTools;
import com.winning.sx.microframework.common.util.SqlTool;
import com.winning.sx.microframework.model.exception.CustomException;
import com.winning.sx.microframework.model.manage.LoginSession;
import com.winning.sx.microframework.model.message.AjaxDataResponseMessage;
import com.winning.sx.microframework.model.message.AjaxTableResponseMessage;
import com.winning.sx.microframework.service.EasyService;
import com.winning.sx.microframework.service.sysmanage.ConstService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/analysis/Comp")
public class CompTableController {
    @Autowired
    EasyService easyService;
    @Autowired
    ConstService service;
    @RequestMapping(value = {"/", ""})
    public ModelAndView init() throws IOException, CustomException, SQLException {
        ModelAndView mv = new ModelAndView("/analysis/compTable");
        List type = service.queryCount("301");
        mv.addObject("type", type);
        List brand = service.queryCount("300");
        mv.addObject("brand", brand);
        List question = service.queryCount("110");
        mv.addObject("question", question);
        Map map = new HashMap();
        map.put("superOrganizationId","0");
        String sql=" select * from BASE_Organization o" +
                " where o.superOrganizationId=0";
        List custPerson = easyService.querySql(sql,map).getDataRows();
        mv.addObject("custPerson", custPerson);
        Map code = new HashMap();
        List QRcode = easyService.queryList("BASE_QRcode", code).getDataRows();
        mv.addObject("QRcode", QRcode);
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = {"/queryCompTable"})
    public AjaxTableResponseMessage queryMessageList(String datas) {
        AjaxTableResponseMessage ajaxTableResponseMessage = new AjaxTableResponseMessage();
        Map<String, String> param = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null)
                param = objectMapper.readValue(datas, Map.class);
            Map paramNew = new HashMap();
           if (!"0".equals(param.get("custPerson"))) {
                String custPerson = param.get("custPerson").toString().trim();
                paramNew.put("custPerson", custPerson);//
            }
            if (!"0".equals(param.get("oneCode"))) {
                String oneCode = param.get("oneCode").toString().trim();
                paramNew.put("oneCode", oneCode);
            }
            if (!"0".equals(param.get("brandCode"))) {
                String brandCode = param.get("brandCode").toString().trim();
                paramNew.put("brandCode", brandCode);
            }
            if (!"0".equals(param.get("model"))) {
                String model = param.get("model").toString().trim().replaceAll(",","");
                paramNew.put("model", model);
            }
            if (!"0".equals(param.get("question"))) {
                String question = param.get("question").toString().trim();
                paramNew.put("question", question);
            }
            if (!"0".equals(param.get("compTableType"))) {
                String compTableType = param.get("compTableType").toString().trim().replaceAll(",","");
                paramNew.put("compTableType", compTableType);
            }
            paramNew.put("b_createTime", param.get("b_createTime"));
            paramNew.put("e_createTime", param.get("e_createTime"));
            String sql;
            if("1".equals(paramNew.get("compTableType").toString())) {
                 sql = "select s.name compare,count(r.id) repairCount  from BUSI_MaintenanceApplication m " +
                        " left join BASE_Equipment e on m.equipmentId=e.id " +
                        " left join BASE_Staff s on s.id=m.applyStaffId " +
                        " left join BUSI_RepairSubmitRecord r on r.repairApplyId=m.id " +
                         "  left join BASE_Organization o on o.id=s.organizationId"+
                        "  where 1=1  " +
                        (paramNew.get("custPerson") != null ? (" and o.id='" + paramNew.get("custPerson") + "'") : "") +
                        (paramNew.get("oneCode") != null ? (" and e.oneCode='" + paramNew.get("oneCode") + "'") : "") +
                        (paramNew.get("brandCode") != null ? (" and e.brandCode='" + paramNew.get("brandCode") + "'") : "") +
                        (paramNew.get("model") != null ? (" and e.model='" + paramNew.get("model") + "'") : "") +
                        (paramNew.get("question") != null ? (" and m.questionTypeCode='" + paramNew.get("question") + "'") : "") +
                        (paramNew.get("b_createTime") != null ? (" and e.createTime>='" + paramNew.get("b_createTime") + "'") : "") +
                        (paramNew.get("e_createTime") != null ? (" and e.createTime<='" + paramNew.get("e_createTime") + "'") : "") +
                        "group by s.name";
            }else if("2".equals(paramNew.get("compTableType").toString())){
                sql = "select e.name compare,count(r.id) repairCount  from BUSI_MaintenanceApplication m " +
                        " left join BASE_Equipment e on m.equipmentId=e.id " +
                        " left join BASE_Staff s on s.id=m.applyStaffId " +
                        " left join BUSI_RepairSubmitRecord r on r.repairApplyId=m.id " +
                        "  left join BASE_Organization o on o.id=s.organizationId"+
                        "  where 1=1  " +
                        (paramNew.get("custPerson") != null ? (" and o.id='" + paramNew.get("custPerson") + "'") : "") +
                        (paramNew.get("oneCode") != null ? (" and e.oneCode='" + paramNew.get("oneCode") + "'") : "") +
                        (paramNew.get("brandCode") != null ? (" and e.brandCode='" + paramNew.get("brandCode") + "'") : "") +
                        (paramNew.get("model") != null ? (" and e.model='" + paramNew.get("model") + "'") : "") +
                        (paramNew.get("question") != null ? (" and m.questionTypeCode='" + paramNew.get("question") + "'") : "") +
                        (paramNew.get("b_createTime") != null ? (" and e.createTime>='" + paramNew.get("b_createTime") + "'") : "") +
                        (paramNew.get("e_createTime") != null ? (" and e.createTime<='" + paramNew.get("e_createTime") + "'") : "") +
                        "group by e.name";
            }else{
                sql = "select c.name compare,count(r.id) repairCount  from BUSI_MaintenanceApplication m " +
                        " left join BASE_Equipment e on m.equipmentId=e.id " +
                        " left join BASE_Staff s on s.id=m.applyStaffId " +
                        " left join BUSI_RepairSubmitRecord r on r.repairApplyId=m.id " +
                        " left join BASE_Const c on c.code=m.questionTypeCode " +
                        "  left join BASE_Organization o on o.id=s.organizationId"+
                        "  where 1=1  " +
                        (paramNew.get("custPerson") != null ? (" and o.id='" + paramNew.get("custPerson") + "'") : "") +
                        (paramNew.get("oneCode") != null ? (" and e.oneCode='" + paramNew.get("oneCode") + "'") : "") +
                        (paramNew.get("brandCode") != null ? (" and e.brandCode='" + paramNew.get("brandCode") + "'") : "") +
                        (paramNew.get("model") != null ? (" and e.model='" + paramNew.get("model") + "'") : "") +
                        (paramNew.get("question") != null ? (" and m.questionTypeCode='" + paramNew.get("question") + "'") : "") +
                        (paramNew.get("b_createTime") != null ? (" and e.createTime>='" + paramNew.get("b_createTime") + "'") : "") +
                        (paramNew.get("e_createTime") != null ? (" and e.createTime<='" + paramNew.get("e_createTime") + "'") : "") +
                        "group by c.name " ;
            }
            DataTable all = easyService.querySql(sql, paramNew);
            ajaxTableResponseMessage.setDatas(all.getDataRows());
            ajaxTableResponseMessage.setTotalSize((int) all.getTotalRowNum());
            ajaxTableResponseMessage.setSuccess(true);
        } catch (Exception e) {
            ajaxTableResponseMessage.setSuccess(false);
            ajaxTableResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxTableResponseMessage;
    }
    @ResponseBody
    @RequestMapping(value = {"/queryModel"})
    public AjaxTableResponseMessage queryModel(String datas) {
        AjaxTableResponseMessage ajaxTableResponseMessage = new AjaxTableResponseMessage();
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                Map param =new HashMap();
                param.put("type",map.get("oneCode"));
                param.put("brand",map.get("brandCode"));
                DataTable all = service.queryModel(param);
                ajaxTableResponseMessage.setDatas(all.getDataRows());
                ajaxTableResponseMessage.setTotalSize((int) all.getTotalRowNum());
                ajaxTableResponseMessage.setSuccess(true);
            }
        }catch (Exception e) {
            ajaxTableResponseMessage.setSuccess(false);
            ajaxTableResponseMessage.setErrorMsg(e.getMessage());
        }
        return ajaxTableResponseMessage;
    }
}

